package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.pojo.RotationChart;
import com.example.demo.service.RotationChartService;
import io.swagger.annotations.*;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;


@Controller
@RequestMapping("/rotationChart")
@Api(value = "轮播图",tags = "轮播图")
public class RotationChartController {

    @Resource
    private RotationChartService rotationChartService;


    @RequestMapping(value = "/deleteByPrimaryKey",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除轮播图",notes = "删除轮播图")
    @ApiImplicitParams({@ApiImplicitParam(name="pictureid",value = "图片id")})
    public String deleteByPrimaryKey(Integer pictureid){
        int res=rotationChartService.deleteByPrimaryKey(pictureid);
        return JSON.toJSONString(res);
    }

    @RequestMapping(value = "/updateByPrimaryKey",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改轮播图",notes = "修改轮播图")
    @ApiImplicitParams({@ApiImplicitParam(name = "pictureid",value = "图片id")})
    public String updateByPrimaryKey(RotationChart record){
        int res=rotationChartService.updateByPrimaryKey(record);
        return JSON.toJSONString(res);
    }

    @RequestMapping(value = "/upload",consumes = "multipart/*",headers = "content-type=multipart/form-data",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "上传轮播图",notes = "上传轮播图")
    public String upload(@ApiParam(value = "上传的文件") MultipartFile file){
        return rotationChartService.Upload(file);
    }
    @RequestMapping("/rotaionChartDo")
    public String rotaionChartDo(Model model){
        List<RotationChart> list=rotationChartService.getAll();
        model.addAttribute("list",list);
        return "link";
    }

    @ApiOperation(value = "查看所有轮播图",notes = "查看所有轮播图")
    @RequestMapping(value = "/getAll",method = RequestMethod.POST)
    public String getAll(){

        return "";
    }
    @RequestMapping(value = "/delRotaionChartId")
    public String delRotaionChartId(Integer pictureid){
        int res=rotationChartService.deleteByPrimaryKey(pictureid);
        return "redirect:/rotationChart/rotaionChartDo";
    }
    @RequestMapping("/addRotaionChartDo")
    public String addRotaionChartDo(){
        return "addRotaionChart";
    }
    @RequestMapping(value = "/addRotaionChart")
    @ApiOperation(value = "上传轮播图",notes = "上传轮播图")
    public String addRotaionChart(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IllegalStateException, IOException {
        if (file!=null) {// 判断上传的文件是否为空
            String path=null;// 文件路径
            String type=null;// 文件类型
            String fileName=file.getOriginalFilename();// 文件原名称
            System.out.println("上传的文件原名称:"+fileName);
            // 判断文件类型
            type=fileName.indexOf(".")!=-1?fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()):null;
            if (type!=null) {// 判断文件类型是否为空
                if ("GIF".equals(type.toUpperCase())||"PNG".equals(type.toUpperCase())||"JPG".equals(type.toUpperCase())) {
                    // 项目在容器中实际发布运行的根路径
                    String realPath=request.getSession().getServletContext().getRealPath("/");
                    File desktopDir = FileSystemView.getFileSystemView() .getHomeDirectory();
                    String desktopPath = desktopDir.getAbsolutePath();
                    // 自定义的文件名称
                    String trueFileName=String.valueOf(System.currentTimeMillis())+fileName;
                    // 设置存放图片文件的路径
                    path=desktopPath+"\\"+trueFileName;
                    RotationChart rotationChart=new RotationChart();
                    rotationChart.setPictureurl(path);
                    rotationChartService.insert(rotationChart);
                    System.out.println("存放图片文件的路径:"+path);
                    // 转存文件到指定的路径
                    file.transferTo(new File(path));
                    System.out.println("文件成功上传到指定目录下");
                }else {
                    System.out.println("不是我们想要的文件类型,请按要求重新上传");
                    return null;
                }
            }else {
                System.out.println("文件类型为空");
                return null;
            }
        }else {
            System.out.println("没有找到相对应的文件");
            return null;
        }
        return "redirect:/rotationChart/rotaionChartDo";

    }
}
